/* 1775. 通过最少操作次数使数组的和相等 */
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */

var minOperations = function (nums1, nums2) {
	const n = nums1.length,
		m = nums2.length
	// 比最大值都大直值返回-1
	if (6 * n < m || 6 * m < n) return -1
	const cnt1 = new Array(7).fill(0)
	const cnt2 = new Array(7).fill(0)
	let diff = 0
	for (const i of nums1) {
		++cnt1[i]
		diff += i
	}
	for (const i of nums2) {
		++cnt2[i]
		diff -= i
	}
	console.log('cnt :>>', cnt1, 'cnt2 :>>', cnt2, 'diff :>>', diff)
	if (diff === 0) return 0
	if (diff > 0) {
		return help(cnt2, cnt1, diff)
	}
	return help(cnt1, cnt2, -diff)
}

const help = (h1, h2, diff) => {
	const h = new Array(7).fill(0)

	for (let i = 1; i < 7; i++) {
		h[6 - i] += h1[i]
		h[i - 1] += h2[i]
	}
	let res = 0
	for (let i = 5; i > 0 && diff > 0; i--) {
		// Math.floor((diff + i - 1) / i) === Math.ceil(diff / i)
		// i 这个数对于抵消diff削减能做到的最大次数
		let t = Math.min(Math.ceil(diff / i), h[i])
		res += t
		diff -= t * i
	}
	return res
}
const res = minOperations([1, 2, 3, 4, 5, 6], [1, 1, 2, 2, 2, 2])
console.log('minOperations : >>', res)
